
#ifndef __DEVICE_H__
#define __DEVICE_H__

#include <stdint.h>
//#include "hal_digitalin.h"
//#include "hal_adc1_dma.h"

#define FACTORY_TEST_TAG  0x55AA
#define SOFTWARE_VERSION  149


#define DI_CMPR_RUN    DI_CHN_1
#define DI_LO_PRESS    DI_CHN_2
#define DI_HI_PRESS    DI_CHN_3
#define DI_DEFROST    DI_CHN_4
#define DI_RESERVE    DI_CHN_5
#define DI_REMOTE_ON  DI_CHN_6

#define DI_FCTRY_TEST  DI_CHN_8

#define TEMP_DISCHG    ADC_CHN_T1      
#define TEMP_SUCTION  ADC_CHN_T2
#define TEMP_OUTDOOR  ADC_CHN_T3

#define AI_LO_PRESS    ADC_CHN_AI_1
#define AI_HI_PRESS    ADC_CHN_AI_2
#define AI_BK_PRESS    ADC_CHN_AI_3

#define DO_FAN_SS_1    DO_CHN_2
#define DO_FAN_SS_2    DO_CHN_3
#define DO_CMPR_HTR    DO_CHN_5

#define DO_VALVE_1    DO_CHN_7
#define DO_VALVE_2    DO_CHN_8

#define TO_EEV_CTRL    TO_CHN_1

#define ERROR_STATE_SIZE  100
//Error Code
#define ERR_COMM    1  //err->comm

#define ERR_PD_FAULT  2  //err->pd_flt 5秒
#define ERR_PS_FAULT  3  //err->ps_flt 5秒
#define ERR_TD_FAULT  4  //err->td_flt
#define EER_TS_FAULT  5  //err->ts_flt

#define ERR_PD_HI    6  //err->pd_hi  41bar 5秒
#define ERR_PS_HI    7  //err->ps_hi
#define ERR_PS_LO    8  //err->ps_lo
#define ERR_TD_HI    9  //err->td_hi  120度60秒
#define ERR_TD_LO    10  //err->td_lo
#define ERR_TSSH_LO  11  //err->tssh_lo  5度 3分钟
#define ERR_FRQ_LO_PD_HI  12  //err->fr_lo_pe_hi 30HZ 34bar

#define ERR_CMPR_DRV  13  //err->drv
#define ERR_PH_UBC    14  //err->ph_ubc
#define ERR_AC_OV      15  //err->ac_ov
#define ERR_AC_UV      16  //err->ac_uv
#define ERR_AC_SCV    17  //err->ac_scv
#define ERR_PFC        18  //err->pfc_oc

#define ERR_PD_LO     19  //err->pd_lo

#define ERR_DI_HI     20  //err->di_hi
#define ERR_DI_LO     21  //err->di_lo

#define ERR_PFC_OTHER  22  
#define ERR_IPM_HI    23  

#define ERR_AC_HI      24
#define ERR_DC_HI      25

#define ERR_DRV_0000_0001  30  
#define ERR_DRV_0000_0002  31  
#define ERR_DRV_0000_0004  32  
#define ERR_DRV_0000_0008  33  
#define ERR_DRV_0000_0010  34  
#define ERR_DRV_0000_0020  35  
#define ERR_DRV_0000_0040  36  
#define ERR_DRV_0000_0080  37  
#define ERR_DRV_0000_0100  38  
#define ERR_DRV_0000_0200  39  
#define ERR_DRV_0000_0400  40  
#define ERR_DRV_0000_0800  41  
#define ERR_DRV_0000_1000  42  
#define ERR_DRV_0000_2000  43  
#define ERR_DRV_0000_4000  44  
#define ERR_DRV_0000_8000  45  
#define ERR_DRV_0001_0000  46  
#define ERR_DRV_0002_0000  47  
#define ERR_DRV_0004_0000  48  
#define ERR_DRV_0008_0000  49  
#define ERR_DRV_0010_0000  50  
#define ERR_DRV_0020_0000  51  
#define ERR_DRV_0040_0000  52  
#define ERR_DRV_0080_0000  53  
#define ERR_DRV_0100_0000  54  

#define ERR_PFC_0000_0001  64  
#define ERR_PFC_0000_0002  65  
#define ERR_PFC_0000_0004  66  
#define ERR_PFC_0000_0008  67  
#define ERR_PFC_0000_0010  68  
#define ERR_PFC_0000_0020  69  
#define ERR_PFC_0000_0040  70  
#define ERR_PFC_0000_0080  71  
#define ERR_PFC_0000_0100  72  
#define ERR_PFC_0000_0200  73  
#define ERR_PFC_0000_0400  74  
#define ERR_PFC_0000_0800  75  
#define ERR_PFC_0000_1000  76  
#define ERR_PFC_0000_2000  77  
#define ERR_PFC_0000_4000  78  
#define ERR_PFC_0000_8000  79  
#define ERR_PFC_0001_0000  80  
#define ERR_PFC_0002_0000  81  
#define ERR_PFC_0004_0000  82  
#define ERR_PFC_0008_0000  83  
#define ERR_PFC_0010_0000  84  
#define ERR_PFC_0020_0000  85  
#define ERR_PFC_0040_0000  86  
#define ERR_PFC_0080_0000  87  
#define ERR_PFC_0100_0000  88  
#define ERR_PFC_0200_0000  89  
#define ERR_PFC_0400_0000  90  
#define ERR_PFC_0800_0000  91  
#define ERR_PFC_1000_0000  92  
#define ERR_PFC_2000_0000  93  


#define CONTROL_STOP 0x01
#define CONTROL_SLOW_INCREASE 0x02
#define CONTROL_FORBID_INCREASER 0x04
#define CONTROL_FORCE_DECREASE 0x08
#define CONTROL_FAST_DECREASE 0x10

typedef enum
{
  ST_STOP= 0,
  ST_INIT= 1,
  ST_RUN= 2,
}DEVICE_STATE;

typedef struct
{
  DEVICE_STATE  state;
  uint8_t pos_set;
  uint8_t pos_step;
}EEV_SET;

typedef struct
{
  unsigned drv:1;
  unsigned ph_ubc:1;
  unsigned ac_ov:1;
  unsigned ac_uv:1;
  unsigned ac_scv:1;
  unsigned pfc_oc:1;
  unsigned pfc_other:1;
  
  unsigned pd_hi:1;
  unsigned ps_hi:1;
  unsigned ps_lo:1;
  unsigned td_hi:1;
  unsigned td_lo:1;
  unsigned tdsh_lo:1;
  unsigned pd_flt:1;
  unsigned ps_flt:1;
  unsigned td_flt:1;
  unsigned ts_flt:1;
  unsigned fr_lo_pe_hi:1;
  unsigned imp_hi:1;
  
  unsigned comm:1;
  unsigned pd_lo:1;
  unsigned di_lo:1;
  unsigned di_hi:1;

  unsigned ac_hi:1;
  unsigned dc_hi:1;
}ERROR_BIT;

void Device_Init(void);
void Device_Control(void);
uint8_t device_Write(uint8_t *pdu, uint8_t pdu_len);
uint8_t device_Write_fan(uint8_t *pdu, uint8_t pdu_len);
void update_device_state(uint8_t *pdu, uint8_t pdu_len);
void update_fan_state(uint8_t *pdu, uint8_t pdu_len);
void update_cmpr_ack_time(int8_t t);
void update_fan_ack_time(int8_t t);

extern uint8_t getNextError(void);
extern uint8_t factory_test;
extern uint8_t factory_test_stage;
extern uint8_t factory_test_error;
extern void SetFlashWriteSource(uint8_t i)  ;
extern uint8_t GetFlashWriteSource(void)  ;
#endif
